package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.Rebate;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import xtom.frame.util.XtomTimeUtil;

/**
 * 类描述：收益适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/4 17:58
 * 修改备注：
 */
public class RebateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Rebate> datas;
    private Context mContext;

    public RebateAdapter(Context context, ArrayList<Rebate> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_rebeat, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0) {//数据为空
            return -1;
        } else//正常情况
            return 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂时没有记录");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setData(datas.get(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.tv_rebeat)
        TextView tvRebeat;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(Rebate rebate) {
            tvRebeat.setText(uuUtils.formatAfterDot2(rebate.getScore()));
            tvDate.setText(XtomTimeUtil.TransTime(rebate.getRegdate(), "yyyy-MM-dd"));
        }
    }
}
